
#======================================================================
import tarfile, os

def untar(fileName):
    
    
    baseName = os.path.basename(fileName)
    dirName = os.path.dirname(fileName)
    
    (folderName, extension) = os.path.splitext(baseName)
    (folderName, extension) = os.path.splitext(folderName)
    
    outputDir = dirName + '\\' + folderName
    
    os.makedirs(outputDir)
    os.chdir(outputDir)
    
    try:  
        tar = tarfile.TarFile.open(fileName, 'r')
#         for tarName in tar.getnames():
#             print tarName
        
        tar.extractall()
        tar.close()
                                                      
    except: 
        print fileName, '*'

#======================================================================
import glob

inputDir = 'G:\\LANDSAT\\GUIANA\\231_59'
fileNames = glob.glob(inputDir + '\\*.tar.gz')

for fileName in fileNames:
    print fileName
    untar(fileName)